<?php require ('setcookie.html'); 
//this sets all variables, that are SUPPOSED TO BE USED FOR TEXT VISIBLE FOR USERS
//all text is encapsulated in the array-variable $LRV
//DO NOT set this variable yourself, if you want to change text, change right in language_related_variables 
require 'language_related_variables.php';
error_reporting(0);
$language_decoder=array('de'=>"german", 'en'=>"english", 'es'=>"spanish", 'ru'=>"russian");
$url_part_array= explode(".", $_SERVER['SERVER_NAME']);
$site_language_short=$url_part_array[0];
if ($site_language_short == 'lingomoto') {$site_language_short = 'en';}
$site_language= $language_decoder[$site_language_short];
if (!$site_language){$site_language="german";$site_language_short="de";}
$LRV=filter_by_language($site_language);


// read configuration
require $LRV["link_configuration"];
require $LRV["link_user"];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html lang="<?php print $site_language_short?>">
<head>
    <title><?php print $LRV['title_tag']['new_vocables'];?></title>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <meta name="description" content="<?php print $LRV['title_tag']['new_vocables'];?>" />
    <meta name="language" content="<?php print $site_language_short?>" />
    <link href="style.css" type="text/css" rel="stylesheet" />
    <link rel="shortcut icon" type="image/x-icon" href="favicon.ico" />
    <link rel="stylesheet" href="autocomplete/jquery.autocomplete.css" type="text/css" />
	<!-- <script type="text/javascript" src="autocomplete/jquery142min.js"></script>
	<script type="text/javascript" src="http://code.jquery.com/jquery-1.5.js"></script>
	 -->
	<link type="text/css" href="css/ui-lightness/jquery-ui-1.8.11.custom.css" rel="Stylesheet" />	
	<script type="text/javascript" src="js/jquery-1.5.1.min.js"></script>
	<script type="text/javascript" src="js/jquery-ui-1.8.11.custom.min.js"></script>
	
	<!--<script type="text/javascript" src="autocomplete/jquery.autocomplete.min.js"></script>
	<script type="text/javascript" src="autocomplete/jquery.autocomplete.pack.js"></script>-->
	<script type="text/javascript" src="autocomplete/jquery.autocomplete.js"></script>
</head>

<?php
$_SESSION['logini'] = isset($_SESSION['logini']) ? $_SESSION['logini'] : false; 
// require ('database_abstraction.php');
define ("TESTING",'');

$customerid = $_SESSION['customerid'];
$otherway = isset($_REQUEST['otherway']) ? $_REQUEST['otherway'] : 0;
$input=$_REQUEST['InputFeld'];
$oldword=$_REQUEST['oldword'];
$checked=get_checked();
$unchecked=$checked=='can'?'learn':'can';

/*if ( $customerid or 
    (!$customerid and !($_SESSION["langold"] or $_SESSION["langold"])) or
    $site_language!=$_SESSION["display_language"])
{ */ //if there is a customerid -> get languages by customerid
//if there is no customerid, but also nothing set for the session -> take standard values for languages
// if site_language is different from the display_language in session -> take standard values
	$lang_info=language_info($site_language_short);
	$can_lang=$lang_info[0]; //'Deutsch' -> this is for the buttons  
	$learn_lang=$lang_info[1];
	
	$lang_info=language_info("en");//standard values for can_lang and learn_lang are set within language_info 
	$can_lang_en=strtolower($lang_info[0]); //'german' -> this is used, when defining the table
	$learn_lang_en=strtolower($lang_info[1]);
	
	$langpair=$lang_info[2];
//}
/*else  //if theres no customerid -> set languages by session
{
	$can_lang=$_SESSION['langold']; 
    $learn_lang=$_SESSION["langnew"];
    $arrLangInfo = getLanguageInfoStandardByLanguagesNotStandard($can_lang,$learn_lang,$site_language);
    $langpair= $arrLangInfo[0];
    $can_lang_en=$arrLangInfo[1];
    $learn_lang_en=$arrLangInfo[2];
} */
$_SESSION['langold']=$can_lang; 
$_SESSION["langnew"]=$learn_lang;
if ( $langpair=='' ) { $langpair = isset($_SESSION['langpair']) ? $_SESSION['langpair'] : 1; }  
$table=find_table();
$checked_lang=$checked=='can'?$can_lang:$learn_lang;
$unchecked_lang=$checked=='can'?$learn_lang:$can_lang;
$selected=$_REQUEST['selected'];
//initialisierung $selected, wenn neue Vokabel
$selected = isset($_REQUEST['select_x']) ? '1000000000' : $selected;
if ($input <> $oldword) {$selected = '1000000000';}

echo_javascript();
echo '<body id="trainer" onLoad="document.getElementById(\'inputfeld\').focus()">';

$link = $_SERVER['PHP_SELF'];//"newvoc1.html"; <-- php_self gibt das momentan ausgeführte dokument an
$langen = 'individual-words.html';
$langes = 'palabras-sueltas.html';
$langru = 'individual-words.html';
$langde = 'einzelne-vokabeln.html';
require ($LRV["link_header"]);
// horizontal column
echo '<div>';n();
require ($LRV["link_horizontal"]);
echo '</div>';n();
// Left Column
echo '<div id="left_col">';n();
require ($LRV["link_vertical"]);
echo '</div>';n();

// <!-- Content -->
echo "<div id='content'>";n();
echo "<div id='block5'>";n();

// request for login
$send = isset($_REQUEST['send']) ? $_REQUEST['send'] : '';
if ($_SESSION['logini']== false and ($send <> '' or $_REQUEST["insert_vocable_list"]) ) {
    require_once ('loginformular.html'); 
}

// if new password, request for change of password
elseif ( $newpass == 1 )
{
$passwo = isset($_REQUEST['passwo']) ? $_REQUEST['passwo'] : '1';
$passwo1 = isset($_REQUEST['passwo1']) ? $_REQUEST['passwo1'] : '1';
$passwo2 = isset($_REQUEST['passwo2']) ? $_REQUEST['passwo2'] : '1';
$ok='';
require_once ($LRV["link_new_password"]); 
}
else 
{
if ( isset($_REQUEST['send']) ) { $submitted_box = $_REQUEST['send']; }

//for ($count=1;$count<=6;$count++) { if ( isset($_REQUEST["submit"."$count"]) ) { $submitted_box=$count; } }
//echo '<form name="xxx" id="vocable_form" action="'.$link.'" method="post" >';n();
if ( isset($submitted_box) or isset($_REQUEST['insert_vocable_list']))
{  
   if (isset($submitted_box)) $insertion_table="wordbox0";
   else $insertion_table= "vocable_lists_words"  ;
   $box = $_REQUEST['order'];
   $list_id= $_REQUEST["list_id"];
   if ($checked == 'learn') {$language='2';} else {$language='1';}
   
   
   insertVocablesTranslations($table,$input,$box,$insertion_table,$list_id);
   //$vocable_inserted='1';
}
if ($_REQUEST["take_row_2"]) //user had to choose between old and new entry and chose new
{
	updateVocablesTranslations($table,$_REQUEST["input"],
	$_REQUEST["order"],$_REQUEST["insertion_table"],$_REQUEST["list_id"]);
}
    
    echo '<form name="xxx" id="vocable_form" action="'.$link.'" method="post" >';n();
    echo '<h2>'.$LRV["page_title"]["new_vocables"].'</h2><br />';n();
       echo '<div class="content_frame2">';n();
       print "<div style='font-size:14px;'><span style='font-weight:bold;' >".$LRV['heading_learnwords1']."</span>".$LRV['heading_learnwords2']."</div><br>";
	   
       echo '<div id="block">';n();
       echo "<div id='langpair' >";n();
	   echo_language_buttons();
	   echo "</div>";n();
       echo "<div id='chglangpair' >";n();
       
	   echo "&nbsp;";
       echo "<span class='link'><a href='".$LRV['link_settings']."'>".$LRV["label_change_language"]."</a></span>"; 
       echo "</div>";   n();
      /*echo "<input type=hidden name=langpair value=". $row['langpair'] . " />";
       $langpair = $row['langpair'];*/
	  
       echo '<div id="linputfeld1">';n();
	   if ($checked=='learn' && $checked_lang <> $learn_lang ) 
	   { $checked_lang = $learn_lang; }	
	   echo $LRV['label_vocable'].' <span class="orange" style="font-weight:bold;" >'. $checked_lang . '</span>';
       echo '</div>';n();
       
	   if ($_REQUEST['checked_learn_x'] or $_REQUEST['checked_can_x'] or $submitted_box) $input_field='';
	   else $input_field=$input;
	   
	   echo '<div id="mySuggest">';
	   echo '<input type= "text" id="inputfeld" class="bigtext" name="InputFeld" value="'. $input_field . '" onClick=this.value="" />';n();
       echo '</div>';
	   echo '<input type="image" class="button2" src="'.$LRV["folder_images"].'/buttons/arrow.gif" name="select" alt="select" title="'.$LRV["title_choose_translations"].'"  />';n();
       echo '<br /><br /></div></div>';n();
    
    
    if ($_REQUEST['InputFeld'] and !$_REQUEST['checked_can_x'] and !$_REQUEST['checked_learn_x'] and !$submitted_box and !$_REQUEST["insert_vocable_list"])  
    {    
	  $translation=echo_translation_results();
	  //print_r($translation);
	  echo ' <div class="block8">';n();
	     print '<br />';  
	  $files=needed_files();
	  // print_r($files);
	  //print 'files';
	  if ($files)echo_questioning_boxes($translation,$files);
      echo ' </div>';n();
      echo '</div>';n();

	// Show wordboxes
      if ($files )
      {    
       echo '<div class="content_frame2">';n();
       print "<div style='font-size:14px;'><span style='font-weight:bold;' >".$LRV['heading_sorting1']."</span>".$LRV['heading_sorting2']."</div><br>";
		  echo '<div id="block9">';
	      print '		<span>'.$LRV["span_sort_into_box"].':</span>';
	      print '         <select name="order" >';
	      print '            <option value="1" >'.$LRV['first_box_title'].' 1 ('.$LRV["recommended"].')</option>';
	      print '            <option value="2" >'.$LRV['first_box_title'].' 2</option>';
	      print '            <option value="3" >'.$LRV['first_box_title'].' 3</option>';
	      print '            <option value="4" >'.$LRV['first_box_title'].' 4</option>';
	      print '            <option value="5" >'.$LRV['first_box_title'].' 5</option>';
	      print '            <option value="6" >'.$LRV['first_box_title'].' 6</option>';
	      print '         </select>';
	      print '<input type="submit" style="margin-left:6px;width:30px;" name="send" value="GO" >';
	      print '   </div>'; 
	      //print $_SESSION["latest_list_id"].'$_SESSION["latest_list_id"]';
          print '<div id="block9">';
	      print '	<span>'.$LRV["span_sort_into_list"].':</span>';
	      print '   	<select name="list_id" >';
		  if ( $_SESSION['logini'] == true )
	      {
		      $res= mysql_query_secure("SELECT * from vocable_lists WHERE customerid = $customerid");
		      while($row=mysql_fetch_array($res))
		      {
		      	$listid=$row['listid'];
		      	$listname= $row['listname'];
		      	
		      	if ($_SESSION["latest_list_id"]==$listid) $boolSelected="selected=\"selected\"";
		      	else $boolSelected="";
		        print '<option value="'.$listid.'" '.$boolSelected.'>'.$listname.'</option>';
		      		
		      }
			  $num_rows = mysql_num_rows($res);
			  if ($num_rows == 0 ) { print '<option value="" >'.$LRV["option_no_lists_yet"].'</option>'; }
	      }
	      else    {print '            <option value="0" >'.$LRV["option_no_lists_yet"].'</option>';
	               $is_disabled='disabled="true"';}  
	      
	      print '       </select>';
	      print '		<input type="submit" style="margin-left:6px;width:30px;" '.$is_disabled.' name="insert_vocable_list" value="GO" >';
	      print '</div>';
		echo '<br /><br />';
		print '</div>';
      }

 
	}
    echo '</form>';
}	

echo "</div>";
echo "</div>";
echo '<div id="adverts">';
echo '<div id="adbox"></div>';
require ('feedback.html');
echo "</div>";
echo '</div>';

require ($LRV["link_footer"]);

function echo_javascript()
{
   global $table;
   echo ' 	<script type="text/javascript">

$(document).ready( function()
  {
     $("#inputfeld").autocomplete("daten.php?table='.$table.'").result(function(event, item, formatted)
	 
	 {
         //$(this).parents().find("form:first").submit();
         $(this).parents().find("#vocable_form").submit();
		 //$("<li>").html("Selected: " +item).appendTo("#result");
     }                                              );
     $("#feedback_button,#feedback_button_cancel").click(function () 
     {
		if ($("#feedback_form").is(":hidden")) 
		{
			$("#feedback_form").show("slide", {direction: "right"}, 1000);
		} 
		else 
		{
			$("#feedback_form").hide("slide", {direction: "right"}, 1000);
		}
	 }
	 );
     
  
  }
  
);
    </script>

</head>';
}
function test_count()
{
   global $count;
   $count++;
   //print $count;b();
}

function b() {print '<br />';} function n() {print "\n";} function bn() {print "<br/>\n";}
function mysql_query_secure($query)
{
   //print $query;
   $result=mysql_query($query);
   if ( ! $result ) { die('Error query : ' . mysql_error()); }
   return $result;
}

function find_table()
{ 
   if (TESTING)echo 'find_table()';
   global $checked,$can_lang_en, $learn_lang_en;
   //$infos=array("Spanisch"=>"spanish", "Deutsch"=>"german", "Englisch"=>"english", "Russisch"=>"russian" ); 
   $checked_lang=$checked=='can'?$can_lang_en:$learn_lang_en;
   $unchecked_lang=$checked=='can'?$learn_lang_en:$can_lang_en;
   $table=$checked_lang.$unchecked_lang;
   return $table;
} 

function get_checked()
{
       if (TESTING)echo 'get_checked()';
       if ( $_REQUEST['checked_can_x']){$a='can';}
	   elseif ( $_REQUEST['checked_learn_x']) {$a='learn';}
	   elseif ( $_REQUEST['checked_before'] == "can" ){$a='can';}
	   else {$a='learn';}
       return $a;

}

function language_info($strTableEnding)
{
       if (TESTING)echo 'language_info()';
	   global $customerid;                     // ...de für deutsche Sprache
	   if (!$customerid) $customerid=-1;
	   $result = mysql_query_secure( "SELECT can$strTableEnding,learn$strTableEnding,langpair FROM settings where customerid = '$customerid'" );
	   if (mysql_num_rows($result)<>0) $line = mysql_fetch_array( $result, MYSQL_ASSOC);
	   return array($line['can'.$strTableEnding],$line['learn'.$strTableEnding],$line['langpair']);

}

/**
 * returns the langpair, the can language and the learn language in standartized form </p>
 * @link 
 * @param can language and learn language in not standartized form </p>
 * <p>standartized form is -> lowercase and english -> e.g.: "english", "russian"</p>
 * <p>not standartized form is -> capitalized and in sites display language ->  e.g.: "Englisch","Russisch"
 * (display language in this example is german)
 */

function getLanguageInfoStandardByLanguagesNotStandard(
			$strCanLangNotStandard,$strLearnLangNotStandard,$strSiteLanguage)
{
	
	$strQuery=
"SELECT 	langpairs_$strSiteLanguage.langpairid,
 			langpairs_english.can,
 			langpairs_english.learn 
FROM 		langpairs_$strSiteLanguage, langpairs_english
WHERE 		langpairs_english.langpairid = langpairs_$strSiteLanguage.langpairid AND
			langpairs_$strSiteLanguage.can = '$strCanLangNotStandard' AND
			langpairs_$strSiteLanguage.learn = '$strLearnLangNotStandard'";
		print $strQuery;
		$objResult=mysql_query_secure($strQuery);
		$objRow=mysql_fetch_array($objResult);
		return array($objRow["langpairid"],strtolower($objRow["can"]),strtolower($objRow["learn"]));
}



function getTranslationsSelected($translation_table)
{
	global $selected,$input; 
	$result=mysql_query_secure(	"SELECT canart,canid,canword,learnart,learnid,learnword ".
   								"FROM $translation_table ".
   								"WHERE canword ='$input' ".
   								"ORDER BY wordid");
   while ($row=mysql_fetch_array($result))
   {
   		switch ($selected[$inner_count])
       	{
            case 1:$arrTranslationsSelected[0]=$row[5];break;
	        case 2:$arrTranslationsSelected[1]=$row[5];break;
            case 3:$arrTranslationsSelected[2]=$row[5];break;
        }
        $inner_count++;
   }
   return $arrTranslationsSelected;
	
} 

function wordbox_insertion_query($translation_table,$input,$boxid,$turned,$insertion_table,$list_id,$language)
{
   global $selected,$langpair,$customerid,$checked_lang;   
   
   $sql="INSERT INTO $insertion_table SET  ";
   if 		($insertion_table=="wordbox0") 
     $sql.= "customerid = '$customerid', boxid =$boxid, notselected = 0,".
            " langpairid = '$langpair', langid = '$language'";
   else if  ($insertion_table=="vocable_lists_words")
     $sql.= "listid = $list_id";
   $inner_count=0;
   
   $result=mysql_query_secure(	"SELECT canart,canid,canword,learnart,learnid,learnword ".
   								"FROM $translation_table ".
   								"WHERE canword ='$input' ".
   								"ORDER BY wordid");
   while ($row=mysql_fetch_array($result))
                                          
   {
      if (!$turned)
      {   
         switch ($selected[$inner_count])
        {
            case 1:$sql.=  ",1art = '{$row[0]}' , 1id = '{$row[1]}' , 1word = '{$row[2]}',".
	        " art1 = '{$row[3]}' , id1 = '{$row[4]}' , word1 = '{$row[5]}'";break;
	        case 2:$sql.= ",art2 = '{$row[3]}' , id2 = '{$row[4]}' , word2 = '{$row[5]}'";break;
            case 3:$sql.=",art3 = '{$row[3]}' , id3 = '{$row[4]}' , word3 = '{$row[5]}'";break;
        }
      }
      else
      {
         switch ($selected[$inner_count])
         {  
            case 1:$sql.=  ",art1 = '{$row[0]}' , id1 = '{$row[1]}' , word1 = '{$row[2]}',".
	        " 1art = '{$row[3]}' , 1id = '{$row[4]}' , 1word = '{$row[5]}'";break;
	        case 2:$sql.= ",2art = '{$row[3]}' , 2id = '{$row[4]}' , 2word = '{$row[5]}'";break;
            case 3:$sql.=",3art = '{$row[3]}' , 3id = '{$row[4]}' , 3word = '{$row[5]}'";break;
	     }  
      } 
   $inner_count++;
   }   
   return $sql;
}

function checkTableEntryExists($strTable, $strColumn,$objEntry,$strAddCondition)
{
    $strQuery= getMysqlQueryString($strTable, $strColumn, $objEntry, $strAddCondition);
	$intResult=mysql_query_secure($strQuery);
	if (mysql_num_rows($intResult)>0) 	return true;
	else 								return false;
}

function getMysqlQueryString($strTable, $strColumn,$objEntry,$strAddCondition)
{
	if (is_object($strTable))$strTable=$strTable->self;
	if (is_numeric($objEntry)) 	$strWhereClause= "WHERE $strColumn= $objEntry";
	else 						$strWhereClause= "WHERE $strColumn= '$objEntry'";
	return "SELECT * FROM $strTable $strWhereClause AND $strAddCondition";
}

function getMysqlQueryResult($strTable, $strColumn,$objEntry,$strAddCondition)
{
	$strQuery=getMysqlQueryString($strTable, $strColumn, $objEntry, $strAddCondition);
	return mysql_query_secure($strQuery);
}

function printHtmlSubmitButton($strName,$strValue)
{
	print "<input type=\"submit\" name=\"$strName\" value=\"$strValue\" />";	
} 
function printHtmlImageButton($strName,$strSource,$strTitle,$strAltTag)
{
	print "<input type=\"image\" src=\"$strSource\" name=\"$strName\" alt=\"$strAltTag\" title=\"$strTitle\" />";
}
function printHtmlHiddenInput($strName,$strValue)
{
	print "<input type=\"hidden\" name=\"$strName\" value=\"$strValue\" />";
}
function printHtmlLink($strLocation,$strTitle)
{
	print "<a href=\"$strLocation\">$strTitle</a>";
}

function printHtmlDivMessage($strMessage)
{
	   echo "<div class='message'>".$strMessage."</div>";
}

function printHtmlFormStart($strName,$strAction,$strId)
{
	if (!$strAction) $strAction= $_SERVER["PHP_SELF"];
	print "<form name=\"$strName\" id=\"$strId\" action=\"$strAction\" method=\"post\" >";	
}
function printHtmlFormEnd()
{
	print "</form>";
}

function updateVocablesTranslations($translation_table,$input,$box,$insertion_table,$list_id)
{
	global $langpair,$language,$customerid,$LRV,$checked;
	$translations=$_REQUEST["translations"];
	
	if ($checked == 'learn') {$language='2';} else {$language='1';}
	if ($insertion_table=="wordbox0")
	{ 
     	$strWhereClause = 	"customerid = '$customerid' AND ". 
     						"boxid = $box AND ". 
     						"notselected = 0 AND ".
            				"langpairid = '$langpair' AND ". 
            				"langid = '$language'";
	}
   	else if  ($insertion_table=="vocable_lists_words")
   	     
   		$strWhereClause = 	"listid = $list_id";
	
   	mysql_query_secure(
	       	"UPDATE $insertion_table SET ".
	       	"word1 = '{$translations[0]}',".
			"word2 = '{$translations[1]}',".
			"word3 = '{$translations[2]}' ".
			"WHERE 1word = '$input' AND ".
			$strWhereClause); 
		
    if ($_REQUEST["otherway"]) 
    { 
    	if ($checked == 'learn') {$language='1';} else {$language='2';} 
    	if ($insertion_table=="wordbox0")
		{ 
     		$strWhereClause = 	"customerid = '$customerid' AND ". 
     							"boxid = $box AND ". 
     							"notselected = 0 AND ".
            					"langpairid = '$langpair' AND ". 
            					"langid = '$language'";
		}
    	mysql_query_secure(
	       	"UPDATE $insertion_table SET ".
	       	"1word = '{$translations[0]}',".
			"2word = '{$translations[1]}',".
			"3word = '{$translations[2]}'".
			"WHERE word1 = '$input' AND ".
			$strWhereClause); 
    }
	
}

function insertVocablesTranslations($translation_table,$input,$box,$insertion_table,$list_id) {

    global $langpair,$language,$customerid,$LRV,$checked; 
    
	if ($insertion_table=="wordbox0") //is it supposed to be inserted into the vocable lists or word boxes?
         $objResult = getMysqlQueryResult("wordbox0", "1word", $input, "customerid = $customerid AND langpairid =$langpair"); //AND langid = $language");
    else if ($insertion_table=="vocable_lists_words")
         $objResult = getMysqlQueryResult("vocable_lists_words","1word",$input ,"listid = $list_id");
    
    $translations=getTranslationsSelected($translation_table);     
    $objRow = mysql_fetch_array($objResult);
    if (mysql_num_rows($objResult)>0  //entry already exists
           and !($objRow["word1"]==$translations[0] and //old vocables-translations-entry is NOT identical to new 
                 $objRow["word2"]==$translations[1] and
                 $objRow["word3"]==$translations[2] )) 
    {
    	$strTableRow = $insertion_table=="wordbox0"? "boxid" : "listname";
        $strInsertObject = $insertion_table=="wordbox0"? $LRV['first_box_title'] : $LRV["list"];
        printHtmlDivMessage
        (
         	$LRV["message_vocable_already_in_table"]." $strInsertObject {$objRow[$strTableRow]}!<br><br>".
         	$LRV["question_keep_translation_line"]."<br><br>"
        );
        
        //shows the two rows and gives possibility to decide which one to keep
        printHtmlFormStart("take_row", "", "");
        print '<table border="0" style="border-collapse:collapse; margin-left:10px;">';
	    print '   <tr>'.
	    			'<th colspan="1">'.$LRV['line'].'</th>'.
	    			'<th colspan ="3">'.$LRV['table_translation_titles']['words'].'</th>'.
			  		'<th colspan ="3">'.$LRV['table_translation_titles']['translations'].'</th></tr>';
	    //row, that is already in the database
	    print   "<tr class='bg'>".
	    		"<td>{$LRV["line1"]}</td>".
	    		"<td>$input</td>".
	    		"<td></td>".
	    		"<td></td>".
	    		"<td>{$objRow["word1"]}</td>".
	    		"<td>{$objRow["word2"]}</td>".
	    		"<td>{$objRow["word3"]}</td>".
	    		"<td><input type='submit' name='take_row_1' value='{$LRV['keep_line']}'></td>".
	    		"</tr>";
	    //new row, that would be created from current input of user
	    print   "<tr>".
	    		"<td>{$LRV["line2"]}</td>".
	    		"<td>$input</td>".
	    		"<td></td>".
	    		"<td></td>".
	    		"<td>{$translations[0]}</td>".
	    		"<td>{$translations[1]}</td>".
	    		"<td>{$translations[2]}</td>".
	    		"<td><input type='submit' name='take_row_2' value='{$LRV['keep_line']}'></td>".
	    		"</tr>";
	  
        print "</table>";
        
        printHtmlHiddenInput("list_id",$_REQUEST["list_id"]);
        printHtmlHiddenInput("order",$_REQUEST["order"]);
        printHtmlHiddenInput("insertion_table", $insertion_table);
        printHtmlHiddenInput("input", $input);
        printHtmlHiddenInput("translations[0]", $translations[0]);
        printHtmlHiddenInput("translations[1]", $translations[1]);
        printHtmlHiddenInput("translations[2]", $translations[2]);
        printHtmlHiddenInput("otherway", $_REQUEST["otherway"]);
        
        printHtmlFormEnd();
        
    }
    else if (mysql_num_rows($objResult)>0  //entry already exists
           and ($objRow["word1"]==$translations[0] and //old vocables-translations-entry IS identical to new one 
                 $objRow["word2"]==$translations[1] and
                 $objRow["word3"]==$translations[2] )
    )
    {
        printHtmlDivMessage($LRV["message_entry_identical"]);         	
    }
    else //otherwise insert new vocable
    {   
    	
    	if ($checked == 'learn') {$language='2';} else {$language='1';}
    	$strQuery=wordbox_insertion_query($translation_table,$input,$box,'',$insertion_table,$list_id,$language);
    	if ($_REQUEST["otherway"]) 
    	{
    		if ($checked == 'learn') {$language='1';} else {$language='2';} 
    		$strQueryReverse= wordbox_insertion_query($translation_table,$input,$box,'1',$insertion_table,$list_id,$language);
    	}
    	if ($strQuery) {  $db_ins = mysql_query_secure( $strQuery ); }
        if ($strQueryReverse) {   $db_ins = mysql_query_secure( $strQueryReverse ); }
        printHtmlDivMessage($LRV["message_vocable_inserted"]);
    }         
}

function echo_language_buttons()
{
  if (TESTING)echo 'echo_language_buttons()';
  global $learn_lang,$checked,$can_lang,$checked_lang,$LRV;
  $image_checked=array('1', '2');
  if ($checked=='can')$choice=True;else $choice=False; 
  echo '<div><input type="image" class="button6" src="'.$LRV["folder_images"].'/buttons/'.$learn_lang.$image_checked[$choice].'.gif" name="checked_learn" value="learn" alt="select" title="'.$LRV["title_choose_language"].'" /></div>';n();
  echo '<div><input type="image" class="button6" src="'.$LRV["folder_images"].'/buttons/'.$can_lang.$image_checked[!$choice].'.gif" name="checked_can" value="can" alt="select" title=" '.$LRV["title_choose_language"].'" /></div>';n();
  echo "<input type='hidden' name='checked_before' value='".$checked. "' />";n();
	

}

function echo_translation_results()
{
    if (TESTING)echo 'echo_translation_result()';
    global $input,$selected,$vocable_inserted,$table,$LRV;

    echo '<div id="block13">';n();  // wie #block
 
    $result=mysql_query_secure("SELECT canword,learnword FROM $table where canword ='$input' COLLATE utf8_bin order by wordid");
    //compares by utf8_bin (binary value) cause its the only one that knows, that umlauts are different (schwul is not like schwül)
    if (mysql_num_rows($result)==0)
	{
	      echo '<div class="content_frame3">';n();
          echo '<div>'.$LRV["message_vocable_not_in_db"].'</div>'; 
          echo '</div>';n();
	   
	}
	elseif (!$vocable_inserted)
	{
	   $num_rows=mysql_num_rows($result)<=3?mysql_num_rows($result):3;
	   
	   if (!isset($selected)) $selected="1000000000";
	   for($line_count=0;$line_count<10;$line_count++)
	   {
		    if ($_REQUEST["select$line_count".'_x'])
			{
				settype($selected, 'string');
				$Res = 0;
 				for($i = 0; $i < strlen($selected); $i ++) {
				$Res = $Res + $selected[$i];
				}
				if ( $Res == 0 ) // Quersumme = 0, d.h. alle Vokabeln deselektiert
				{
					$selected[$line_count]= "1";
				}
				if ( $Res == 1 ) // Quersumme = 1, d.h. bisher eine Vokabel markiert
				{
					if ( substr($selected, $line_count, 1) == 0 )
						{ 
							$selected[$line_count]= "2";
						}
					elseif ( substr($selected, $line_count, 1) == 1 ) 	
						{ 
							$selected[$line_count]= "0";
						}
				}
				if ( $Res == 3 ) // Quersumme = 3, d.h. bisher zwei Vokabeln markiert
				{
					if ( substr($selected, $line_count, 1) == 0 )
						{ 
							$selected[$line_count]= "3";
						}
					elseif ( substr($selected, $line_count, 1) == 1 ) 	
						{ 
							$selected=str_replace("2",'1',$selected);
							$selected=str_replace("3",'2',$selected);
							$selected[$line_count]= "0";
						}
					elseif ( substr($selected, $line_count, 1) == 2 ) 	
						{ 
							$selected=str_replace("3",'2',$selected);
							$selected[$line_count]= "0";
						}
					elseif ( substr($selected, $line_count, 1) == 3 ) 	
						{ 
							$selected[$line_count]= "0";
						}
				}
				if ( $Res == 6 ) // Quersumme = 6, d.h. bisher drei Vokabeln markiert
				{
					if ( substr($selected, $line_count, 1) == 1 ) 	
						{ 
							$selected=str_replace("2",'1',$selected);
							$selected=str_replace("3",'2',$selected);
							$selected[$line_count]= "0";
						}
					elseif ( substr($selected, $line_count, 1) == 2 ) 	
						{ 
							$selected=str_replace("3",'2',$selected);
							$selected[$line_count]= "0";
						}
					elseif ( substr($selected, $line_count, 1) == 3 ) 	
						{ 
							$selected[$line_count]= "0";
						}
				}
			}
	   } 							


       echo '<div id="block7">';n();
       print "<div style='font-size:14px;'><span style='font-weight:bold;' >".$LRV["heading_translations1"]."</span>".$LRV["heading_translations2"]."</div><br>";

	   $line_count=0;
	   while($line =mysql_fetch_array($result))
   	   {
   	   	  if ($input ==$line['canword'])
   	   	  {
	          $input=$line['canword']; //Korrektur Groß-/Kleinschreibung
		      $word=$line['learnword'];
		      $translation[$line_count+1]=$word;
			  $count=1;
		 	  $sel=$selected[$line_count]<>'0'?'1':'2';
              echo "<div class='check$count' >";n();
			  echo "<input type='image' src='{$LRV["folder_images"]}/buttons/favibutton$sel.gif' name='select$line_count' alt='{$LRV["alt_title_translation"]}' title=' {$LRV["alt_title_translation"]} ' class='button3' />";n();
			  echo "</div>";n();
			  echo "<div class='word' >".$word."</div>";n();
   	   	  }
		  $line_count++;
	   }
	   echo "<input type='hidden' name='selected' value='". $selected . "' />";n();
	   echo "<input type='hidden' name='oldword' value='". $input . "' />";n();
       echo '</div>';
    }
	return $translation;
}	

function echo_questioning_boxes($translation,$files)
{
   if (TESTING)echo 'echo_questioning_boxes()'; 
   global $customerid,$input,$selected,$table,$LRV;
   echo '<div class="heading4"><b>'.$LRV["heading_input"].'</b>';n();
   echo '<div class="heading5">'. $input . '</div>';n();
   echo '</div>';n();
   echo '<div class="heading6"><b>'.$LRV["heading_answer"].'</b>';n();
   for ($count=1;$count<=3;$count++)
	{
	   if (strpos($selected,"$count")===0 or strpos($selected,"$count")>0)
	   {
	      $translation_ordered[$count]=$translation[strpos($selected,"$count")+1];
		  echo '<div class="heading5">'. $translation[strpos($selected,"$count")+1] . '</div>';n();
       }
	}
    echo '</div>';
    // audio test
    if ( !$_REQUEST['test_x'])
    {               
        
        echo '<div class="check5" >';n();
        echo "<div class='check4' >";n();
		echo "<input type='image' src='{$LRV["folder_images"]}/buttons/button_quick_find.gif' name='test' alt='{$LRV['alt_title_listening_test']}' title='{$LRV['alt_title_listening_test']}' />";n();
        echo '</div>';n();
        echo '<div class="textsmall2" >&nbsp;</div><div class="textsmall" >'.$LRV['alt_title_listening_test'].'</div>';n();
        bn();
        echo '</div>';n();
    }               
    else
    {
	    
        $noCache = time();
 
        // create test file
		
        $file_name=create_mp3_file($turned,$files);

        // flash player for testing
        echo '<div class="check5" >';n();
        echo '<div class="check4" >';n();
        echo '<object type="application/x-shockwave-flash" data="player_mp3.swf" width="227" height="15">';n();
        echo '<param name="movie" value="player_mp3.swf?noCache=$noCache" />';n();
        echo '<param name="bgcolor" value="#f4f4f4" />';n();
		echo '<param name="FlashVars" value="mp3='.$file_name.'&amp;autoplay=1" />';
        echo '</object>';n();
        echo '</div>';n();
        bn();bn();  echo '</div>';n();
    }


    // show checkbox for the other way
    echo '<div class="check5" >';n();
    echo '<div class="check4" ><input type=Checkbox name=otherway value="1" checked="checked" />';n();
    echo '</div>';n();
    echo '<div class="textsmall2" >&nbsp;</div><div class="textsmall" >'.$LRV["lable_turn_request"].'</div>';n();
    echo '</div>';n();

    // Show selected words the other way
    echo '<div class="heading4"><b>'.$LRV["heading_input"].'</b>';n();
    
	for ($count=1;$count<=3;$count++)
	{
	   if (strpos($selected,"$count")===0 or strpos($selected,"$count")>0)
	   {
	      echo '<div class="heading5">'. $translation[strpos($selected,"$count")+1] . '</div>';n();
	   }
	}
    echo '</div>';n();
    echo '<div class="heading6"><b>'.$LRV["heading_answer"].'</b>';n();
    echo '<div class="heading5">'. $input . '</div>';n();
    echo '</div>';n();

    // audio test
    if ( !$_REQUEST['test2_x'])
    {
        echo '<div class="check5" >';n();
        echo "<div class='check4' ><input type='image' src='{$LRV["folder_images"]}/buttons/button_quick_find.gif' name='test2' alt='{$LRV['alt_title_listening_test']}' title=' {$LRV['alt_title_listening_test']} ' />";n();
        echo '</div>';n();
        echo '<div class="textsmall2" >&nbsp;</div><div class="textsmall" >'.$LRV['alt_title_listening_test'].'</div>';n();
        bn();echo '</div>';
    }
    else
    {
        // create test file
		$turned=1;
        $file_name=create_mp3_file($turned,$files);
        // flash player for testing
        echo '<div class="check5" >';n();
        echo '<div class="check4" >';n();
        echo '<object type="application/x-shockwave-flash" data="player_mp3.swf" width="200" height="15">';n();
        echo '<param name="movie" value="player_mp3.swf" />';n();
        echo '<param name="bgcolor" value="#f4f4f4" />';n();
        echo '<param name="FlashVars" value="mp3='.$file_name.'&amp;autoplay=1" />';
		echo '</object>';n();
        echo '</div>';n();
        echo '</div>';n();
        
}
}
function needed_files()
{
   global $table,$customerid,$translation,$input,$selected,$site_language_short; 
   
   //print_r ($translation_ordered);
   for ($count=1;$count<=3;$count++)
	{
	   if (strpos($selected,"$count")===0 or strpos($selected,"$count")>0)
	   {
	      $translation_ordered[$count]=$translation[strpos($selected,"$count")+1];
		 /* echo '<div class="heading5">'. $translation[strpos($selected,"$count")+1] . '</div>';n(); */
       } 
	}
	switch ( $table ) {
    case 'germanenglish' : $folder1 = 'ger1443'; $folder2 = 'eng2573'; break;
    case 'germanspanish' : $folder1 = 'ger1443'; $folder2 = 'spa7023'; break;
    case 'englishgerman' : $folder1 = 'eng2573'; $folder2 = 'ger1443'; break;
    case 'spanishgerman' : $folder1 = 'spa7023'; $folder2 = 'ger1443'; break;
    case 'germanrussian' : $folder1 = 'ger1443'; $folder2 = 'rus7261'; break;
    case 'russiangerman' : $folder1 = 'rus7261'; $folder2 = 'ger1443'; break;
     }

	 $ifile = glob("test/". $customerid . "lingomoto*");
        if ( $ifile[0] <> '' ) 
        {
            array_map('unlink', $ifile);      
        }
	if ( $site_language_short == 'en' )
	{
	$argv[0] = "mp3/generaI243/mediumbreak.mp3"; 
    for ($count=1;$count<=3;$count++)
	{if ($translation_ordered[$count])	$argv[$count]="mp3/".$folder2."/".$translation_ordered[$count].".mp3";}
    $argv[4] = "mp3/". $folder1 . "/". $input. ".mp3";  
	// delete the abbreviations
	$argv[4] = str_replace(" (f)", "", $argv[4]);
	$argv[4] = str_replace(" (m)", "", $argv[4]);
	$argv[4] = str_replace(" (adj)", "", $argv[4]);
	$argv[4] = str_replace(" (vb)", "", $argv[4]);
	$argv[4] = str_replace(" (n)", "", $argv[4]);
	$argv[4] = str_replace(" (adv)", "", $argv[4]);
	$argv[5] = "test/".$customerid."lingomoto".rand(1000,9999).".mp3";
    $argv[6] = "mp3/generaI243/smallbreak.mp3";
	} else {
	$argv[0] = "../mp3/generaI243/mediumbreak.mp3"; 
    for ($count=1;$count<=3;$count++)
	{if ($translation_ordered[$count])	$argv[$count]="../mp3/".$folder2."/".$translation_ordered[$count].".mp3";}
    $argv[4] = "../mp3/". $folder1 . "/". $input. ".mp3";  
	// delete the abbreviations
	$argv[4] = str_replace(" (f)", "", $argv[4]);
	$argv[4] = str_replace(" (m)", "", $argv[4]);
	$argv[4] = str_replace(" (adj)", "", $argv[4]);
	$argv[4] = str_replace(" (vb)", "", $argv[4]);
	$argv[4] = str_replace(" (n)", "", $argv[4]);
	$argv[4] = str_replace(" (adv)", "", $argv[4]);
	$argv[5] = "test/".$customerid."lingomoto".rand(1000,9999).".mp3";
    $argv[6] = "../mp3/generaI243/smallbreak.mp3"; }
   //print 'needed_files()';b();
   // print_r($argv); 
    if (!is_file($argv[4]) or !(is_file($argv[1]) or is_file($argv[2]) or is_file($argv[3])))
	   return False;
	else return $argv;   


}

function create_mp3_file($turned,$argv)
{  
   
   if ($turned) $content_files=array($argv[1],$argv[2],$argv[3],$argv[0],$argv[4]);
	else $content_files=array($argv[4],$argv[0],$argv[1],$argv[2],$argv[3]);
	//print_r($content_files);
	foreach ($content_files as $file)
	{
	   if (is_file($file)) $file_content.=file_get_contents($argv[6]).file_get_contents($file);
    }	
    file_put_contents( $argv[5],$file_content); 
	$file_name=$argv[5];    
    //print $file_name; 
    return $file_name;	

}
class vocable_list_words
{
	var $id="id"; 	
	var	$listid="listid"; 
	var	$oneart ="1art";	
	var	$oneword 	="1word";
	var	$oneid 	="1id";
	var	$twoart 	="2art";
	var	$twoword 	="2word";
	var	$twoid 	="2id";
	var	$threeart 	="3art";
	var	$threeword 	="3word";
	var	$threeid 	="3id";
	var	$art1 	="art1";
	var	$word1 	="word1";
	var	$id1 	="id1";
	var	$art2 	="art2";
	var	$word2 	="word2";
	var	$id2 	="id2";
	var	$art3 	="art3";
	var	$word3 	 ="word3";
	var	$id3 		="id3";
	var	$timestamp="timestamp";
} 

class wordbox0
{
	var $self="wordbox0" ;
	var $customerid="customerid"; 	
	var	$boxid="boxid"; 
	var $langpairid="langpairid"; 	
	var	$langid="langid";
	var $notselected="notselected"; 	
	var	$boxbefore="boxbefore";  
	var	$lastdownload ="lastdownload";	
	var	$oneword 	="1word";
	var	$oneid 	="1id";
	var	$twoart 	="2art";
	var	$twoword 	="2word";
	var	$twoid 	="2id";
	var	$threeart 	="3art";
	var	$threeword 	="3word";
	var	$threeid 	="3id";
	var	$art1 	="art1";
	var	$word1 	="word1";
	var	$id1 	="id1";
	var	$art2 	="art2";
	var	$word2 	="word2";
	var	$id2 	="id2";
	var	$art3 	="art3";
	var	$word3 	 ="word3";
	var	$id3 		="id3";
	var	$timestamp="timestamp";
}	
class ratings
{
	var	$listid 	="listid";
	var	$total_votes 	="total_votes";
	var	$total_value 	="total_value";
	var	$used_customer_ids 	="used_customer_ids";
	 
}
$mysqlVocableListsWords= new vocable_list_words();
$mysqlWordbox0= new wordbox0();
$mysqlRatings= new ratings();
$_SESSION["display_language"]=$site_language;

?>

</body>
 

</html>